// Chrome扩展核心类型定义

import type {
  AnalysisOptions,
  ExtensionSettings,
  ActivityItem,
  PageAnalysisResult,
  UserPreferences
} from './app';

export interface TabInfo {
  id: number;
  title: string;
  url: string;
  favicon?: string;
  active?: boolean;
  windowId?: number;
  index?: number;
  status?: 'loading' | 'complete';
}

export interface WindowInfo {
  id: number;
  type: 'normal' | 'popup' | 'panel' | 'app' | 'devtools';
  focused: boolean;
  state: 'normal' | 'minimized' | 'maximized' | 'fullscreen';
  tabs?: TabInfo[];
}

// Chrome消息类型
export interface MessageBus {
  // 页面分析
  ANALYZE_PAGE: {
    tabId: number;
    options?: AnalysisOptions;
  };

  // 侧边栏控制
  OPEN_SIDE_PANEL: {
    tabId?: number;
  };

  CLOSE_SIDE_PANEL: {};

  TOGGLE_SIDE_PANEL: {
    tabId?: number;
  };

  // 页面加载事件
  PAGE_LOADED: {
    tabId: number;
    url: string;
    title: string;
  };

  // 文本选择事件
  TEXT_SELECTED: {
    text: string;
    url: string;
    tabId: number;
  };

  // 设置更新
  UPDATE_SETTINGS: {
    settings: Partial<ExtensionSettings>;
  };

  // 功能切换
  TOGGLE_FEATURE: {
    feature: string;
    enabled: boolean;
  };

  // 侧边栏状态更新
  UPDATE_SIDE_PANEL_STATE: {
    isOpen: boolean;
  };

  // 强制关闭侧边栏
  FORCE_CLOSE_SIDE_PANEL: {};

  // 内部关闭侧边栏消息（用于background和sidepanel通信）
  INTERNAL_CLOSE_SIDE_PANEL: {
    tabId?: number;
  };
}

export type MessageType = keyof MessageBus;

export interface ChromeMessage<T extends MessageType = MessageType> {
  action: T;
  data?: MessageBus[T];
  tabId?: number;
  timestamp?: number;
}

// Chrome存储类型
export interface StorageData {
  settings: ExtensionSettings;
  recentActivity: ActivityItem[];
  pageAnalysis: Record<string, PageAnalysisResult>;
  userPreferences: UserPreferences;
}

export type StorageKey = keyof StorageData;

// Chrome权限
export type ChromePermission =
  | 'storage'
  | 'activeTab'
  | 'scripting'
  | 'contextMenus'
  | 'sidePanel'
  | 'tabs'
  | 'bookmarks'
  | 'history';

// 上下文菜单
export interface ContextMenuInfo {
  menuItemId: string;
  selectionText?: string;
  pageUrl?: string;
  frameUrl?: string;
  linkUrl?: string;
  mediaType?: string;
}